package com.ldq.imooc.basics.socket;

import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * @Author : 李道泉
 * @Description : URL读取网页内容
 * @Date : 2018/4/23 14:04
 * @Modified by :
 */
public class NetTest3 {

    public static void main(String[] args) {

        try {
            URL baidu = new URL("http://www.baidu.com");
            //通过URL的openstream()方法获取URL对象所表示的资源的字节输入流
            InputStream is = baidu.openStream();
            //字节输入流转换为字符输入流
            InputStreamReader isr = new InputStreamReader(is);
            //添加缓冲
            BufferedReader br = new BufferedReader(isr);
            //将内容输出到本地  问题：图片文字混合，用哪个流较好
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("F:\\IO\\index.html")));

            String data = br.readLine();
            while (data != null) {

                System.out.println(data);
                //写入本地
                bw.write(data);
                bw.flush();
                data = br.readLine();
            }

            bw.close();
            br.close();
            isr.close();
            is.close();

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
